package com.cunyme;

import java.io.IOException;
import java.util.Map;

import org.jsoup.Connection;
import org.jsoup.Jsoup;

import android.app.Activity;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.text.Html;
import android.text.method.LinkMovementMethod;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;

public class Blackboard_my_grades extends Activity
{
	@Override
    public void onCreate(Bundle savedInstanceState)
    {		
        super.onCreate(savedInstanceState);

		SQLiteDatabase cuny_meDB = openOrCreateDatabase("cuny_me.db", Context.MODE_PRIVATE, null);
        helper_functions helpers = new helper_functions();
		Map <String, String> blackboard_cookies = helpers.getcookies("blackboard", cuny_meDB);
		cuny_meDB.close();
		
		// This creates the main layout...
		ScrollView parent_layout = new ScrollView(this);
    	parent_layout.setBackgroundColor(0xffffffff);
		
    	HorizontalScrollView grades_layout = new HorizontalScrollView(this);
    	grades_layout.setBackgroundColor(0xffffffff);
    	
    	parent_layout.addView(grades_layout);
    	
    	
    	LinearLayout child_layout = new LinearLayout(this); 

    	child_layout.setOrientation(LinearLayout.VERTICAL);
    	child_layout.setBackgroundColor(0xffffffff);
    	LinearLayout.LayoutParams bboard_params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,
    			LinearLayout.LayoutParams.WRAP_CONTENT); 
		grades_layout.addView(child_layout);
    	
    	TableLayout table = new TableLayout(this); 

    	table.setOrientation(LinearLayout.VERTICAL);
    	table.setBackgroundColor(0xffffffff);
    	TableLayout.LayoutParams grades_params = new TableLayout.LayoutParams(TableLayout.LayoutParams.FILL_PARENT,
    			TableLayout.LayoutParams.WRAP_CONTENT); 
		
		try
		{
			Connection grades_conn = Jsoup.connect("https://bbhosted.cuny.edu/webapps/gradebook/do/student/viewGrades").data("course_id", getIntent().getExtras().getString("course_id")).cookie("JSESSIONID", blackboard_cookies.get("JSESSIONID")).cookie("session_id", blackboard_cookies.get("session_id")).cookie("ObSSOCookie", blackboard_cookies.get("ObSSOCookie")).cookie("NSC_100211.qspe2_wjq2:443", blackboard_cookies.get("NSC_100211.qspe2_wjq2:443")).cookie("BIGipServerbbhosted_ssl_pl", blackboard_cookies.get("BIGipServerbbhosted_ssl_pl"));
			org.jsoup.nodes.Document grades_doc = grades_conn.timeout(10*1000).post();
			String grades_html = grades_doc.html();
			String[] grades_manip = grades_html.split("<!-- End InstructionBar-->");
			
			TextView title = new TextView(this);
			title.setText(Html.fromHtml("<b>View Grades</b><br />"));
			title.setId(1);
			title.setLayoutParams(bboard_params);
			title.setTextColor(0xff000000);
			child_layout.addView(title);
			
			TextView subtitle = new TextView(this);
			subtitle.setText(Html.fromHtml("Your grades for this Course<br />"));
			subtitle.setId(2);
			subtitle.setLayoutParams(bboard_params);
			subtitle.setTextColor(0xff000000);
			child_layout.addView(subtitle);
			
			if (grades_html.indexOf("No Grade Center columns exist.") >= 0)
			{
				TextView nogrades = new TextView(this);
				nogrades.setText("No Grade Center columns exist.");
				nogrades.setId(3);
				nogrades.setLayoutParams(bboard_params);
				nogrades.setTextColor(0xff000000);
				child_layout.addView(nogrades);
			}
			else
			{
				String [] grades_table = grades_manip[1].split("</table>");
				String [] grade_rows = grades_table[0].split("<tr>");
				

				for (int i = 1; i < grade_rows.length; i++)
				{
					TableRow tr = new TableRow(this);
					tr.setId(100+i);
					table.addView(tr);
					String [] table_elements = grade_rows[i].split("t. class=");
					for (int j = 1; j < table_elements.length; j++)
					{
						if (j == 2)						// Ignoring Details column
							continue;
						table_elements[j] = "<" + table_elements[j];
						table_elements[j] = table_elements[j].replaceAll("<.*?>", "");
						table_elements[j] = table_elements[j].replaceAll("<.*?", "");
						table_elements[j] = table_elements[j].trim();
						table_elements[j] = table_elements[j] + "    ";
						TextView column_headers = new TextView(this);
						column_headers.setText(table_elements[j]);
						column_headers.setId(1000 + j);
						column_headers.setTextColor(0xff000000);
						column_headers.setGravity(17);
						tr.addView(column_headers);
//						Toast.makeText(getApplicationContext(), table_elements[j], Toast.LENGTH_LONG).show();	
					}
				}

			}
			
			child_layout.addView(table);
			
			setContentView(parent_layout);
		} 
		catch (IOException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
			Toast.makeText(getApplicationContext(), "Unable to Establish Connection.  Please try again.", Toast.LENGTH_SHORT).show();	
			finish();
		}
    }
}
